const { con, Result } = require('./common');

let Commodity_gets = async () => {
	let res = new Result()
	let sql = `select * from commodity`
	let [rows] = await con.execute(sql)
	res.data = rows
	return res
}
let Commodity_temp_delete = async (business_id, business_password, commodity_temp_id, admin_id, admin_password) => {
	let res = new Result()
	let rows = [];
	if (business_id) {
		let sql = `select business_password from business where business_id = '${business_id}'`
		[rows] = await con.execute(sql)
	}
	if (admin_id) {
		let sql = `select admin_password from admin where admin_id = ${admin_id}`;
		[rows] = await con.execute(sql);
	}
	if (rows[0].business_password !== business_password) {
		res.code = 40002
		res.msg = '密码错误'
	} else {
		let sql3 = `delete from image where commodityi_temp_id = ${commodity_temp_id}`;
		let [rows3] = await con.execute(sql3);
		let sql2 = `delete from commodity_temp where commodity_temp_id = '${commodity_temp_id}'`
		let [rows1] = await con.execute(sql2)
		res.msg = '删除成功'
	}
	return res
}

let Commodity_delete = async (business_id, business_password, commodity_id, admin_id, admin_password) => {
	let res = new Result()
	let rows = [];
	if (business_id) {
		let sql = `select business_password from business where business_id = '${business_id}'`
		console.log(sql);
		[rows] = await con.execute(sql)
	}
	if (admin_id) {
		let sql = `select admin_password from admin where admin_id = ${admin_id}`;
		[rows] = await con.execute(sql);
	}
	if (rows[0].business_password !== business_password) {
		res.code = 40002
		res.msg = '密码错误'
	} else {
		let sql3 = `delete from image where commodityi_id = ${commodity_id}`;
		let [rows3] = await con.execute(sql3);
		let sql2 = `delete from commodity where commodity_id = '${commodity_id}'`
		let [rows1] = await con.execute(sql2)
		res.msg = '删除成功'
	}
	return res
}
let Commodity_temp = async (business_id, commodity_temp_title, filename, commodity_temp_descript, commodity_temp_money, commodity_temp_t, commodity_temp_addres, time) => {
	let res = new Result()
	let sql = `select * from commodity_temp`;
	let sql4 = `select business_header from business where business_id = ${business_id}`;
	let [rows4] =await con.execute(sql4);
	if (rows4[0].business_header == null) {
		console.log(rows4[0].business_header);
		res.msg = '请先实名认证！';
		res.code = 40010;
	} else {
		console.log(rows4[0].business_header);
		let [rows] = await con.execute(sql)
		if (rows.length == 0) {
			let sql1 = `insert into commodity_temp values(1,'${commodity_temp_title}','${filename}',${business_id},'${commodity_temp_descript}',${commodity_temp_money},'${time}','${commodity_temp_t}','${commodity_temp_addres}')`;
			let [rows1] = await con.execute(sql1)
		} else {
			let sql1 = `insert into commodity_temp values(${rows[rows.length - 1].commodity_temp_id + 1},'${commodity_temp_title}','${filename}',${business_id},'${commodity_temp_descript}',${commodity_temp_money},'${time}','${commodity_temp_t}','${commodity_temp_addres}')`;
			let [rows1] = await con.execute(sql1)
		}
		let sql2 = `select * from commodity_temp where business_id = ${business_id}`;
		let [rows2] = await con.execute(sql2);
		res.msg = '等待审核';
		res.data = rows2[rows2.length - 1];
	}
	return res;
}

let Commodity_temp_get = async () => {
	let res = new Result()
	let sql = `select * from commodity_temp`;
	let [rows] = await con.execute(sql)
	res.data = rows;
	return res
}

let Commodity = async (commodity_temp_id) => {
	let res = new Result()
	let sql = `select * from commodity_temp where commodity_temp_id = ${commodity_temp_id}`;
	let [rows] = await con.execute(sql);
	let sql1 = `select * from commodity`;
	let [rows1] = await con.execute(sql1)
	let sql2 = `select * from business where business_id = ${rows[0].business_id}`;
	let [rows2] = await con.execute(sql2);
	if (rows1.length == 0) {
		let sql3 = `insert into commodity values(1,'${rows[0].commodity_temp_title}','${rows[0].commodity_temp_pic}',${rows[0].business_id},0,0,'${rows[0].commodity_temp_t}',${rows[0].commodity_temp_money},'${rows[0].commodity_temp_time}','${rows[0].commodity_temp_addres}','${rows[0].commodity_temp_describe}',23,NULL)`;
		let [rows3] = await con.execute(sql3);
	} else {
		let sql3 = `insert into commodity values(${rows1[rows1.length - 1].commodity_id + 1},'${rows[0].commodity_temp_title}','${rows[0].commodity_temp_pic}',${rows[0].business_id},0,0,'${rows[0].commodity_temp_t}',${rows[0].commodity_temp_money},'${rows[0].commodity_temp_time}','${rows[0].commodity_temp_addres}','${rows[0].commodity_temp_describe}',23,NULL)`;
		let [rows3] = await con.execute(sql3);
	}
	let sql3 = `delete from commodity_temp where commodity_temp_id = ${commodity_temp_id}`;
	let [rows3] = await con.execute(sql3);
	return res
}
let Commodity_temp_get_one = async (business_id) => {
	let res = new Result();
	let sql = `select * from commodity_temp where business_id = ${business_id}`;
	let [rows] = await con.execute(sql);
	res.data = rows;
	return res;
}
let Commodity_defate = async (commodity_temp_id) => {
	let res = new Result();
	let sql = `delete from commodity_temp where commodity_temp_id = ${commodity_temp_id}`;
	let [rows] = await con.execute(sql);
	return res;
}
module.exports = { Commodity_defate, Commodity_temp_get_one, Commodity_gets, Commodity_temp, Commodity_temp_get, Commodity, Commodity_delete, Commodity_temp_delete }
